package com.freedy.concurrent.OptimisticLock;

import lombok.Data;
import sun.misc.Unsafe;

/**
 * @author Freedy
 * @date 2021/4/24 15:35
 */
public class UnsafeTest {
    public static void main(String[] args) throws NoSuchFieldException {
        Unsafe unsafe = UnsafeGetter.getUnsafe();
        long idOffset = unsafe.objectFieldOffset(Teacher.class.getDeclaredField("id"));
        long nameOffset = unsafe.objectFieldOffset(Teacher.class.getDeclaredField("name"));
        Teacher teacher = new Teacher();
        unsafe.compareAndSwapInt(teacher,idOffset,0,1);
        unsafe.compareAndSwapObject(teacher,nameOffset,null,"张三");

        System.out.println(teacher);
    }
}

@Data
class Teacher {
    volatile int id;
    volatile String name;
}
